package rta.util
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.formatters.DateFormatter;
	
	import spark.components.Label;
	
	public class Cronometro extends Label
	{
		private const TIMER_INTERVAL:int = 10;
		private var baseTimer:int;
		private var dateFormatter:DateFormatter;
		private var t:Timer;
		
		public function Cronometro()
		{
			super();
			
			dateFormatter = new DateFormatter();
			dateFormatter.formatString = "JJ:NN:SS";
			text = "00:00";
			t = new Timer(TIMER_INTERVAL);
			t.addEventListener(TimerEvent.TIMER, updateTimer);
		}
		
		private function updateTimer(evt:TimerEvent):void {
			var ms:Number = parentApplication.getTimerApp() - baseTimer;
			var d:Date = new Date(0, 0, 0, 0, 0, 0, ms);
			text = dateFormatter.format(d);
		}
		
		public function startTimer():void {
			baseTimer = parentApplication.getTimerApp();
			t.start();
		}
		
		/**Retorna el tiempo transcurrido en segundos
		 * **/
		public function stopTimer():int {
			t.stop();
			var ms:Number = parentApplication.getTimerApp() - baseTimer;
			
			return ms / 1000;
		}
	}
}